<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
    	<meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
		
		<link rel="stylesheet" href="libs/bootstrap/css/bootstrap.min.css"/>
		
		<script src="libs/jquery.min.js"></script>
		<script src="libs/bootstrap/js/bootstrap.min.js"></script>
		
		<title>提问</title>
		
		<style>
			.header{
				width: 40px;
				height: 40px;
				border-radius: 50%;
			}
		</style>
		
		
		
	</head>
	<body>
		
		<nav class="navbar navbar-inverse" role="navigation">
		  	<div class="container">
			    <div class="navbar-header">
			      	<a class="navbar-brand" href="#">智游教育</a>
			    </div>
			
			    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
			      	<ul id="right-list" class="nav navbar-nav navbar-right">
				        
				        <li id="menu-or-regist" class="dropdown">
				          	<a href="#" class="dropdown-toggle" data-toggle="dropdown">
				          		<span class="glyphicon glyphicon-user"></span>
				          		<span id="account">用户名</span>
				          		<span class="caret"></span>
				          	</a>
				          	<ul class="dropdown-menu" role="menu">
					            <li><a href="/upload.html">上传头像</a></li>
					            <li><a href="#" onclick="logoutBtnClick(event)">退出登录</a></li>
				          	</ul>
				        </li>
			      	</ul>
			    </div>
		  	</div>
		</nav>
		
		
		<div class="container">
			
			<form id="ask-form" role="form">
				
				<textarea required name="question" class="form-control"></textarea>
				
			</form>
			<br />
			<button onclick="commitBtnClick(event)" class="btn btn-success form-control">提交问题</button>
		</div>
		
		
		
		
	</body>
	
	<script src="libs/jquery.cookie.js"></script>
	
	<script>
		if($.cookie("token")){
			//已经登录
			$("#account").text($.cookie("account"));
			
			var htmlStr = '<li style="line-height: 46px;">'+
	        '<img class="header" onerror="headerError(event)" src="/headers/'+$.cookie("account")+'.jpg"/>'+
	        '</li>';
			
			$(htmlStr).appendTo($("#right-list"));
			
		}else{
			//未登录
			location.href = "/";
		}
		
		
		
		//退出登录的函数
		function logoutBtnClick(e){
			e.preventDefault();
			
			//删除token
			$.removeCookie("token");
			
			//刷新本页
			location.reload();
		}
		
		
		//头像请求失败时的事件函数
		function headerError(e){
			
			//当头像加载失败时，使用默认头像。
			e.target.src = "/headers/default/timg.jpg";
		}
		
		
		//提交问题的函数
		function commitBtnClick(e){
			e.preventDefault();
			
			var data = $("#ask-form").serialize();
			
			$.post("/ask-api",data,function(resData){
				alert(resData.msg);
				if(resData.err == 0){
					location.href = "/";
				}
			});
			
		}
		
	</script>
	
	
</html>
